<?php
/**
 * +----------------------------------------------------------------------
 * | 模型
 * +----------------------------------------------------------------------
 *                      _ooOoo_
 *                     o8888888o            | AUTHOR: 杜云
 *                     88" . "88            | EMAIL: 987772927@qq.com
 *                     (| -_- |)            | QQ: 987772927
 *                     O\  =  /O            | WECHAT: 987772927
 *                  ____/`---'\____
 *                .'  \\|     |//  `.
 *               /  \\|||  :  |||//  \
 *              /  _||||| -:- |||||-  \
 *              |   | \\\  -  /// |   |
 *              | \_|  ''\-/''  |   |
 *              \  .-\__  `-`  ___/-. /
 *            ___`. .'  /-.-\  `. . __
 *         ."" '<  `.___\_<|>_/___.'  >'"".
 *        | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *        \  \ `-.   \_ __\ /__ _/   .-` /  /
 *   ======`-.____`-.___\_____/___.-`____.-'======
 *                      `=-='
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);
namespace App\Model;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\Annotation\CacheEvict;
use Hyperf\Database\Model\SoftDeletes;
/**
 * @property int $id 主键
 * @property string $institution_name 机构名称
 * @property string $institution_code 机构编码
 * @property string $institution_address 机构地址
 * @property string $hospital_phone 院方电话
 * @property string $hospital_type 医院类型
 * @property int $bed_count 床位数
 * @property int $doctor_count 医生人数
 * @property int $nurse_count 护士人数
 * @property string $medical_subject 诊疗科目
 * @property string $hospital_grade 医院等级
 * @property int $is_tertiary_hospital 是否为三甲医院
 * @property string $hospital_property 医院性质
 * @property int $is_medical_insurance_point 是否为医保定点单位
 * @property int $department_count 部门数
 * @property int $status 状态
 * @property int $created_user 创建者
 * @property string $remark 备注
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 软删除字段
 */
class Institution extends BaseModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'institutions';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $hidden = ['deleted_at'];

    protected $fillable = ['id','pid','level','institution_image','institution_name', 'institution_code', 'institution_address','lng','lat','config', 'status','sort', 'created_user', 'remark', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int','pid' => 'int','level' => 'int','driver_status' => 'int','status' => 'integer','sort' => 'integer', 'created_user' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
    /**
     * [createuser 创建者]
     * @return [type] [description]
     */
    public function createuser()
    {
        return $this->hasOne(User::class, 'id', 'created_user');
    }
    /**
     * [vehicle 关联车辆]
     * @return [type] [description]
     */
    public function vehicle()
    {
        return $this->hasMany(Vehicle::class, 'mid', 'id');
    }
    /**
     * [online_num 关联车辆]
     * @return [type] [description]
     */
    public function online_num()
    {
        return $this->hasMany(Vehicle::class, 'mid', 'id');
    }
    /**
     * [nopos_num 关联车辆]
     * @return [type] [description]
     */
    public function nopos_num()
    {
        return $this->hasMany(Vehicle::class, 'mid', 'id');
    }
    /**
     * [lnglat 经纬度记录]
     * @return [type] [description]
     */
    public function lnglat()
    {
        return $this->hasMany(VehicleLnglatRecord::class, 'mid', 'id');
    }
    /**
     * [getIns description]
     * @param  [type] $where [description]
     * @return [type]       [description]
     * @Cacheable(prefix="Ins",ttl=3600, value="#{where.id}",listener="Ins")
     */
    public static function getIns($where = []):array
    {
        $M = self::query()->withCount(['vehicle',
            'online_num'=>function ($query){
                $query->where([['lng','<>',''],['device_staus','=',1]]);
            },
            'nopos_num'=>function ($query){
                $query->where([['lng','=','']]);
            }
        ]);
        if(isset($where['id'])) $M->where('id',$where['id']);
        if(isset($where['key'])) $M->where('institution_name', 'like', '%'.$where['key'].'%');
        $list = $M->where($where)->orderBy('sort','asc')->orderBy('id','desc')->get();
        $list->map(function($item){
        //     $total = $item->vehicle()->count();
        //     $online_num =$item->vehicle()->where([['lng','<>',''],['updated_at','>',date("Y-m-d H:i:s", strtotime("-5 minutes"))]])->count();
        //     $nopos_num = $item->vehicle()->where([['lng','=','']])->count();
        //     $item['online_num'] = $online_num;
        //     $item['nopos_num'] = $nopos_num;
            $item['level_num'] = $item['vehicle_count']-$item['online_num_count']-$item['nopos_num_count'];
        });
        return ListToTree($list);
    }
}